package org.leviatan.definator.gui.swingmodels;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import javax.swing.table.AbstractTableModel;

import org.leviatan.definator.core.model.Field;

public class FieldTableModel extends AbstractTableModel {
	
	private String[] columnNames = {"Id","Name", "Type", "Length", "Rec. reference"};
	
	private Object[][] tableData;
	
	
	private static final long serialVersionUID = -8962042943625322979L;
	
	public void setData(Map fieldDefs) {
		this.tableData = new Object[fieldDefs.size()][5];
		int col = 0;
		for (Iterator i = fieldDefs.values().iterator(); i.hasNext(); ) {
			Field fld = (Field) i.next();
			String id = fld.getId();
			String name = fld.getName();
			String type = fld.getType();
			int length = fld.getLength();
			String recRef = fld.getRecordRef();
			
			tableData[col][0] = id;
			tableData[col][1] = name;
			tableData[col][2] = type;
			tableData[col][3] = length;
			tableData[col][4] = recRef;
			
			col++;
		}
	}
	
	public void initData(Collection fldModels) {
		this.tableData = new Object[fldModels.size()][5];
		int col = 0;
		for (Iterator i = fldModels.iterator(); i.hasNext(); ) {
			Field fld = (Field) i.next();
			
			String id = fld.getId();
			String name = fld.getName();
			String type = fld.getType();
			int length = fld.getLength();
			String recRef = fld.getRecordRef();
			
			tableData[col][0] = id;
			tableData[col][1] = name;
			tableData[col][2] = type;
			tableData[col][3] = length;
			tableData[col][4] = recRef;
			
			col++;
		}
	}
	
	public String getColumnName(int i) {
		return this.columnNames[i];
	}
	
	public int getColumnCount() {
		return this.columnNames.length;
	}

	public int getRowCount() {
		return tableData.length;
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		return this.tableData[rowIndex][columnIndex];
	}

}
